local skel = fk.CreateSkill {
  name = "emo__lingren",
}

Fk:loadTranslationTable{
  ["emo__lingren"] = "凌人",
  [":emo__lingren"] = "每回合限两次，当你使用伤害牌指定目标后，你可以猜测一个目标角色的手牌是否有基本牌、锦囊牌或装备牌。至少猜对一项则此牌对其伤害+1；至少猜对两项则其无法响应此牌；猜对三项则你获得〖奸雄〗和〖行殇〗直到你下回合开始。",

  ["#emo__lingren-choose"] = "凌人：猜测其中一名目标角色的手牌中是否有基本牌、锦囊牌或装备牌",
  ["#emo__lingren-invoke"] = "凌人：是否对 %dest 发动，猜测其手牌中是否有基本牌、锦囊牌或装备牌",
  ["#emo__lingren-choice"] = "凌人：猜测 %dest 的手牌中是否有基本牌、锦囊牌或装备牌",
  ["emo__lingren_basic"] = "有基本牌",
  ["emo__lingren_trick"] = "有锦囊牌",
  ["emo__lingren_equip"] = "有装备牌",
  ["#emo__lingren_result"] = "%from 猜对了 %arg 项",

  ["$emo__lingren1"] = "秉凌人之盛气，试青锋于万里！",
  ["$emo__lingren2"] = "斩宵小于沙场，绝仇雠于疆野！",
}

skel:addEffect(fk.TargetSpecified, {
  anim_type = "offensive",
  times = function (_, player)
    return 2 - player:usedSkillTimes(skel.name)
  end,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player:usedSkillTimes(skel.name) < 2 and
    data.use and data.firstTarget and data.card.is_damage_card
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(data.use.tos, function (to) return not to.dead end)
    local tos = {}
    if #targets == 1 then
      if room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#emo__lingren-invoke::" .. targets[1].id  }) then
        tos = {targets[1]}
      end
    elseif #targets > 1 then
      tos = room:askToChoosePlayers(player, {
        min_num = 1, max_num = 1, targets = targets, skill_name = skel.name, prompt = "#emo__lingren-choose",
      })
    end
    if #tos > 0 then
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local choices = {"emo__lingren_basic", "emo__lingren_trick", "emo__lingren_equip"}
    local yes = room:askToChoices(player, {
      min_num = 0, max_num = 3, choices = choices, skill_name = skel.name, cancelable = false,
      prompt = "#emo__lingren-choice::" .. to.id,
    })
    for _, value in ipairs(yes) do
      table.removeOne(choices, value)
    end
    local right = 0
    for _, id in ipairs(to.player_cards[Player.Hand]) do
      local str = "emo__lingren_"..Fk:getCardById(id):getTypeString()
      if table.contains(yes, str) then
        right = right + 1
        table.removeOne(yes, str)
      else
        table.removeOne(choices, str)
      end
    end
    right = right + #choices
    room:sendLog{
      type = "#emo__lingren_result",
      from = player.id,
      arg = tostring(right),
      toast = true,
    }
    if right == 0 then return end
    if right > 0 then
      data.extra_data = data.extra_data or {}
      data.extra_data.emo__lingrenDamage = data.extra_data.emo__lingrenDamage or {}
      table.insert(data.extra_data.emo__lingrenDamage, to.id)
    end
    if right > 1 then
      player:drawCards(2, skel.name)
      local list = data.use.disresponsiveList or {}
      table.insertIfNeed(list, to)
      data.use.disresponsiveList = list
    end
    if right > 2 then
      local skills = table.filter({"emo__jianxiong", "emo__xingshang"}, function (s)
        return not player:hasSkill(s, true)
      end)
      if #skills > 0 then
        local mark = player:getTableMark("emo__lingren_skills")
        table.insertTableIfNeed(mark, skills)
        room:setPlayerMark(player, "emo__lingren_skills", mark)
        room:handleAddLoseSkills(player, table.concat(skills, "|"))
      end
    end
  end,
})

skel:addEffect(fk.DamageInflicted, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if player.dead or data.card == nil or target ~= player then return false end
    local room = player.room
    local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
    if not use_event then return false end
    local use = use_event.data
    return use.card == data.card and use.extra_data and table.contains(use.extra_data.emo__lingrenDamage or {}, player.id)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    data:changeDamage(1)
  end,
})

skel:addEffect(fk.TurnStart, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("emo__lingren_skills") ~= 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local skills = player:getMark("emo__lingren_skills")
    room:setPlayerMark(player, "emo__lingren_skills", 0)
    room:handleAddLoseSkills(player, "-"..table.concat(skills, "|-"))
  end,
})

return skel
